#!/bin/bash
##############################################################
# File Name：monitr_zookeeper_kafka.sh
# Version :V1 . 
# Author: wangXiaoFeng 
# Email: 1289832464@qq.com
# Created Time ：2023-02-07 15:43:04
# Description：
#############################################################

#Please specify the kafka binHome directory 
kafkaBinPath='/opt/app/kafka_2.11-2.1.0/bin'
#Please specify the configuration file path of kafka
kafkaConfPath='/opt/app/kafka_2.11-2.1.0/config/server.properties'
#monitr_cronTab  
monitr_cronTab='*/1 * * * *'


curDate=`date +"%F %T"`
kafkaConfFile=`echo ${kafkaConfPath}|awk -F '/' '{print $NF}'`
kafkaPid=`ps -ef|grep -v grep |grep "${kafkaBinPath}"|grep "${kafkaConfFile}"|awk '{print $2}'`


echo -e "\n[Info]:curDate:${curDate}"
#checking kafka
if [ -z "${kafkaPid}" ];then
   echo -e "[Error]:kafka is not runing,\n[Info]:Begin start kafka...... "
   cd ${kafkaBinPath}
   nohup ./kafka-server-start.sh -daemon ${kafkaConfPath} 1>/dev/null 2>&1 &
   sleep 3
   newKafkaPid=`ps -ef|grep -v grep |grep "${kafkaBinPath}"|grep "server.properties"|awk '{print $2}'`
   if [ -z "${newKafkaPid}" ];then
        echo -e "[Error]:Kafka startup failed" 
   else
        echo -e "[Info]:Kafka started successfully,Pid is:${newKafkaPid}"
   fi  
else
  echo -e "[Info]:Kafka is runing,Pid is:${kafkaPid} "
fi
echo -e "\n"

#add crontab
curPath=$(cd `dirname $0`; pwd)
fileName=`echo "$0"|awk -F/ '{print $NF}'`
touch /var/spool/cron/root
sed -i "/${fileName}/d" /var/spool/cron/root
echo "${monitr_cronTab} ${curPath}/${fileName} >> /tmp/${fileName}".log >> /var/spool/cron/root
/sbin/service crond restart $1>/dev/null 2>&1
